package com.titan.demo.camel.sample.eip.split;

import java.util.ArrayList;
import java.util.List;

import org.apache.camel.CamelContext;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;

import com.titan.demo.camel.utils.RunCamel;

public class SplitListTest {
	public static void main(String[] args) {
        try {
        	RouteBuilder route = createRouteBuilder();
        	createRunCamel().run(route);
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	
	private static RunCamel createRunCamel() {
		return new RunCamel() {
			protected void execute(CamelContext context,
					ProducerTemplate producer, ConsumerTemplate consumer)
					throws Exception {
				List<String> items = new ArrayList<String>();
				for (int i = 0; i < 5; i++) {
					items.add("值【"+i+"】");
				}
				
				producer.sendBody("direct:in", items);
			}
		};
	}
	
	private static RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            public void configure() throws Exception {
                from("direct:in")
                .split(body(List.class))
                .process(new Processor() {
					public void process(Exchange exchange) throws Exception {
						System.out.println("环境信息： size=["+exchange.getProperty("CamelSplitSize")+"]  index=["+exchange.getProperty("CamelSplitIndex")+"]  isComplete=["+exchange.getProperty("CamelSplitComplete")+"]");
						System.out.println("循环数据："+exchange.getIn().getBody());
					}
                })
                ;
            }
        };
    }
}
